﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SAUlot
{
    class Sprite
    {
        class SubSprite
        {
            private float mScale = 0.3f;
            private Texture2D obj;
            private string assetName;
            private float rotation;
            public float Rotation
            {
                get { return rotation; }
                set { rotation = value; }
            }
            public Vector2 position;
            public Vector2 origin;
            public Rectangle size;

            public SubSprite(string name)
            {
                assetName = name;
            }

            public void LoadContent(ContentManager theContentManager)
            {
                rotation = 0;
                obj = theContentManager.Load<Texture2D>(assetName);
                size = new Rectangle(0, 0, (int)(obj.Width * mScale), (int)(obj.Height * mScale));
            }
            public void Draw(SpriteBatch theSpriteBatch)
            {
                theSpriteBatch.Draw(obj,
                position,
                new Rectangle(0, 0, obj.Width, obj.Height),
                Color.White, rotation, new Vector2(obj.Width * origin.X,obj.Height* origin.Y), mScale, SpriteEffects.None, 0);
            }

            public float Scale
            {
                get { return mScale; }
                set
                {
                    mScale = value;
                    size = new Rectangle(0, 0, (int)(obj.Width * mScale), (int)(obj.Height * mScale));
                }
            }
        }

        private SubSprite Tlo;
        private SubSprite Korpus;
        private SubSprite TylniSter;
        private SubSprite Wartosc_zadana;
        private SubSprite Kierunek;
        private SubSprite Wolant;
        private SubSprite TloWolant;

        private float mScale = 0.4f;
        public Vector2 canvasCenter = new Vector2(0, 0);

        public void LoadContent(ContentManager theContentManager)
        {
            Tlo = new SubSprite("..\\Content1\\bg");
            Tlo.LoadContent(theContentManager);
            Tlo.Scale = 2.5f;
            Tlo.position = canvasCenter + Vector2.Zero;
            Tlo.origin = new Vector2(0.5f, 0.5f);

            Korpus = new SubSprite("..\\Content1\\samolot");
            Korpus.LoadContent(theContentManager);
            Korpus.Scale = mScale;
            Korpus.position = canvasCenter + Vector2.Zero;
            Korpus.origin = new Vector2(0.5f, 0.5f);

            TylniSter = new SubSprite("..\\Content1\\tylnister");
            TylniSter.LoadContent(theContentManager);
            TylniSter.Scale = mScale;
            TylniSter.position = canvasCenter + Vector2.Zero;
            TylniSter.origin = new Vector2(1.0f, 0.5f);

            Wartosc_zadana = new SubSprite("..\\Content1\\zadana");
            Wartosc_zadana.LoadContent(theContentManager);
            Wartosc_zadana.Scale = mScale;
            Wartosc_zadana.position = canvasCenter + Vector2.Zero;
            Wartosc_zadana.origin = new Vector2(0.0f, 0.5f);

            Kierunek = new SubSprite("..\\Content1\\predkosc");
            Kierunek.LoadContent(theContentManager);
            Kierunek.Scale = mScale;
            Kierunek.position = canvasCenter + Vector2.Zero;
            Kierunek.origin = new Vector2(0.0f, 0.5f);

            TloWolant = new SubSprite("..\\Content1\\wolant_bg");
            TloWolant.LoadContent(theContentManager);
            TloWolant.Scale = mScale;
            TloWolant.position = new Vector2(0, canvasCenter.Y*2) + Vector2.Zero;
            TloWolant.origin = new Vector2(0.0f, 1.0f);

            Wolant = new SubSprite("..\\Content1\\wolant");
            Wolant.LoadContent(theContentManager);
            Wolant.Scale = mScale;
            Wolant.position = new Vector2(0, canvasCenter.Y * 2) + new Vector2(TloWolant.size.Width/2,0);
            Wolant.origin = new Vector2(0.5f, 1.0f);
        }

        public void Draw(SpriteBatch theSpriteBatch)
        {
            // Tlo
            Tlo.Draw(theSpriteBatch);
            // Korpus
            Korpus.Draw(theSpriteBatch);
            // Kierunek
            Kierunek.Draw(theSpriteBatch);
            // Wartość zadana
            Wartosc_zadana.Draw(theSpriteBatch);
            // TylniSter
            TylniSter.position = Vector2.Transform(new Vector2(-100.0f,0.0f), Matrix.CreateRotationZ(Korpus.Rotation));
            TylniSter.position += canvasCenter;
            TylniSter.Draw(theSpriteBatch);
            // TloWolant
            TloWolant.Draw(theSpriteBatch);
            // Wolant
            Wolant.Draw(theSpriteBatch);

        }

        public float Scale
        {
            get { return mScale; }
            set {
                mScale = value;
                Korpus.Scale = mScale;
                TylniSter.Scale = mScale;
                Wartosc_zadana.Scale = mScale;
                Kierunek.Scale = mScale;
                Wolant.Scale = mScale;
                TloWolant.Scale = mScale;
            }
        }

        public void Update(double[] state)
        {
            // 0:alpha 1:q 2:theta 3:delta 4:alpha_zadane
            Korpus.Rotation = (float)(-state[2]);
            Kierunek.Rotation = (float)(state[0]-state[2]);
            TylniSter.Rotation = (float)(state[3]-state[2]);
            Tlo.Rotation = 0;
            Wartosc_zadana.Rotation = (float)(state[4]);

            Wolant.Rotation = (float)(-state[3]);
        }
    }
}
